package com.joeyzh.base;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.joey.ui.util.ImageDelegate;
import com.joey.ui.util.ImageShapeUtil;
import com.joey.ui.util.TextDelegate;
import com.joey.ui.widget.MySpinnerView;
import com.joeyzh.widget.PopUpSpinner;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

/**
 * Created by Joey on 2018/10/30.
 * 用于基础的ViewModel类，用于处理View和model层的数据绑定
 *
 * @author Joey
 */

public class ItemViewModel<Bean> extends IViewModel<Bean> implements TextDelegate, ImageDelegate {

    private final String NULL = "null";

    public ItemViewModel(Bean bean) {
        super(bean);
    }

    @Override
    public <T extends View> ItemViewModel bindViewItem(String key, T view) {
        super.bindViewItem(key, view);
        Object value = getValue(key);
        if (value == null) {
            return this;
        }
        if (TextUtils.isEmpty(value.toString())) {
            return this;
        }
        if (view instanceof TextView) {
            setText((TextView) view, value.toString());
        }
        if (view instanceof PopUpSpinner) {
            ((PopUpSpinner) view).setSeletedById(value.toString());
        }
        if (view instanceof MySpinnerView) {
            ((MySpinnerView) view).setSeletedById(value.toString());
        }
        if (view instanceof ImageView) {
            setImageView((ImageView) view, value.toString());
        }
        return this;
    }


    @SuppressWarnings("TypeParameterUnusedInFormals")
    @Nullable
    public <T extends View> T getView(String key) {
        return (T) viewMap.get(key);
    }

    public void bindBeanValues() {
        if (viewMap == null || viewMap.isEmpty()) {
            return;
        }
        for (String key : viewMap.keySet()) {
            View view = viewMap.get(key);
            setBeanValue(key, getText(view));
        }
        entityBean.toString();
    }

    @Override
    public void updateEntityBean(Bean bean) {
        super.updateEntityBean(bean);
        if (viewMap == null || viewMap.isEmpty()) {
            return;
        }
        for (String key : viewMap.keySet()) {
            View view = viewMap.get(key);
            bindViewItem(key, view);
        }

    }

    public void cleanViewValue() {
        if (viewMap == null || viewMap.isEmpty()) {
            return;
        }
        for (String key : viewMap.keySet()) {
            View view = viewMap.get(key);
            if (view instanceof TextView) {
                setText((TextView) view, "");
            }
            if (view instanceof PopUpSpinner) {
                ((PopUpSpinner) view).clearSelected();
            }
            if (view instanceof ImageView) {
                setImageView((ImageView) view, "");
            }
        }
    }

    public ItemViewModel setBeanValue(String key, String value) {
        setValue(key, value);
        return this;
    }

    public void setValue(String key, String value) {
        try {
            Class<Bean> cls = (Class<Bean>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            setValue(cls, key, value);
        } catch (Exception e) {
            e.printStackTrace();
            setValue(entityBean.getClass(), key, value);
        }
    }

    public <E> void setValue(Class<E> cls, String key, String value) {
        try {
            Method[] m = cls.getMethods();
            for (int i = 0; i < m.length; i++) {
                if (("set" + key).toLowerCase().equals(m[i].getName().toLowerCase())) {
                    m[i].invoke(entityBean, value);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("TypeParameterUnusedInFormals")
    @Nullable
    public <T extends Object, E> T getValue(Class<E> cls, String key) {
        try {
            Method[] m = cls.getMethods();
            for (int i = 0; i < m.length; i++) {
                if (("get" + key).toLowerCase().equals(m[i].getName().toLowerCase())) {
                    return (T) m[i].invoke(entityBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("TypeParameterUnusedInFormals")
    @Nullable
    public <T extends Object> T getValue(String key) {
        try {
            return getValue(entityBean.getClass(), key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getText(View view) {
        String text = "";
        if (view == null) {
            return text;
        }
        try {
            if (view.getVisibility() != View.VISIBLE) {
                return "";
            }
            if (view instanceof PopUpSpinner) {
                text = ((PopUpSpinner) view).getSelectedIds().trim();
                return text;
            }
            if (view instanceof MySpinnerView) {
                text = ((MySpinnerView) view).getSelectedIds().trim();
                return text;
            }
            if (view instanceof TextView) {
                text = ((TextView) view).getText().toString().trim();
                return text;
            }
        } catch (Exception e) {

        }

        return text;
    }

    @Override
    public void setText(TextView textView, int res) {
        if (res <= 0) {
            textView.setText("");
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(res);
        }
    }

    @Override
    public void setText(TextView textView, CharSequence text) {
        if (!TextUtils.isEmpty(text) && !NULL.equalsIgnoreCase(text.toString())) {
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        } else {
            textView.setText("");
        }
    }

    @Override
    public void setImageView(ImageView imageView, int res) {
        if (res > 0) {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(res);
        }
    }

    @Override
    public void setImageView(ImageView imageView, String url) {
        if (!TextUtils.isEmpty(url)) {
            imageView.setVisibility(View.VISIBLE);
            ImageShapeUtil.setImage(imageView, url);
        }
    }
}
